/*
 * Copyright (c) 2012-2012 XXXX XXXXXX, Inc.
 * XXXX Road, XXXX, XXXX State, XXXX Post code, XXXX Country.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of XXXX
 * XXXXXX, Inc. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with XXXX.
 * 
 * History
 * Date        Ver Author        Change Description
 * ----------- --- ------------- ----------------------------------------
 * 08 Jun 2013 002 Karl          Update xpath
 */
package test.com.sms.selenium.singlepoint.util;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import test.com.sms.selenium.framework.SeleniumPageUtil;
import test.com.sms.selenium.util.SeleniumUtil;

public class SpHotmailInboxPageUtil extends SeleniumPageUtil {
	
	public final static By INBOX_LINK_INBOX = By.id("00000000-0000-0000-0000-000000000001");
	public final static By INBOX_DIV_SUBJECTS = By.xpath("//li[@msg='msg']");
	public final static By INBOX_DIV_MESSAGE_BODY = By.id("mpf0_MsgContainer");
	
	public static void waitForPageToLoad(){
		SeleniumUtil.waitOnElements(new By[]{INBOX_LINK_INBOX});
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(INBOX_LINK_INBOX));
		// wait for all mail to be listed
		SeleniumUtil.waitForElementsToLoadCompletely(INBOX_DIV_SUBJECTS, 10, 200); 
	}
	
	/**
	 * Find the mail with indicated subject.
	 * */
	public static WebElement findMailWithTextInSubject(String subject){
		List<WebElement> mailLinks = SeleniumUtil.findElements(INBOX_DIV_SUBJECTS);
		return mailLinks.size() > 0 ? mailLinks.get(0) : null;
	}
	
	/** open one mail*/
	private static void enterMail(WebElement mailLink){
		SeleniumUtil.click(mailLink);
		// wait or a fixed milliseconds to wait for mail show
		SeleniumUtil.waitForFixedMilliseconds(2000);
	}
	
	/** Assume a mail is open is current page, get the text in it.*/
	private static String getMailText(){
		List<WebElement> messageBodys = SeleniumUtil.findElements(INBOX_DIV_MESSAGE_BODY);
		if(messageBodys.size() == 0){
			throw new AssertionError("The message body part is empty. Please check if network performs ok.");
		}
		
		for(WebElement loopEle : messageBodys){
			// there might be several message bodies in the shell content div, but only one is displayed. 
			if(loopEle.isDisplayed()){ 
				return loopEle.getText();
			} 
		}
		throw new AssertionError("Can not find the message body text. Please check if network performs ok.");
	}
	
	/**
	 * Assume current page is the mail list page. enter one mail and verify. 
	 * */
	public static void assertMailContainsText(WebElement mailLink, String incidentNumber){
		enterMail(mailLink);
		String mailText = getMailText();
		SeleniumUtil.assertTrue("Can't find the expected mail with incident number [" + 
			incidentNumber + "].", mailText.contains(incidentNumber));
	}
}
